package bitekeji.day2;

import java.io.File;
import java.io.IOException;

public class CopyDirectory {
    static String source = "D:\\课程\\2022-05-08-Java19-22班-线程总结";
    static String dest = "D:\\课程\\2022-05-09-Java19班-IO\\目标";

    public static void main(String[] args) throws IOException {
        File srcFile = new File(source);
        if (!srcFile.isDirectory()) {
            System.out.println("源不是目录");
            return;
        }
        File destFile = new File(dest);
        if (destFile.exists()) {
            System.out.println("目标路径已经存在");
            return;
        }

        traversal(srcFile, destFile);
    }

    private static void traversal(File dir, File destFile) throws IOException {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                String path1 = file.getCanonicalPath();
                String relative = path1.substring(source.length());
                String path2 = dest + relative;
                File file2 = new File(path2);
                file2.mkdirs();
                traversal(file, destFile);
            } else if (file.isFile()) {
            }
        }
    }
}
